#define  _CRT_SECURE_NO_WARNINGS 1
#include "seqList.h"
void SLInit(SL* ps)
{
	ps->arr = NULL;
	ps->size = 0;
	ps->capacity = 0;
}

void SLDestory(SL* ps)
{
	free(ps->arr);
	ps->capacity = 0;
	ps->size = 0;
}

void SLPrint(SL* ps)
{
	int temp =0;
	while (temp < ps->size)
	{
		printf("%d ", ps->arr[temp]);
		temp++;
	}
	printf("\n");
}

void SLCheckCapacity(SL* ps)
{
	if (ps->size == ps->capacity)
	{
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		SLDataType* temp = (SLDataType*)realloc(ps->arr, newcapacity * sizeof(SLDataType));
		if (temp == NULL)
		{
			printf("realloc fail\n");
			exit(-1);
		}
		ps->arr = temp;
		ps->capacity = newcapacity;
	}
}

void SLPushBack(SL* ps,SLDataType elem)
{
	/*SLCheckCapacity(ps);
	ps->arr[ps->size] = elem;
	ps->size++;*/
	SLInsert(ps, ps->size, elem);
}

void SLPopBack(SL* ps)
{
	/*assert(ps->size > 0);
	ps->size--;*/
	SLErase(ps, ps->size-1);
}

void SLPushFront(SL* ps, int elem)
{
	/*SLCheckCapacity(ps);
	int end = ps->size - 1;
	while (end >= 0)
	{
		ps->arr[end + 1] = ps->arr[end];
		end--;
	}
	ps->arr[0] = elem;
	ps->size++;*/
	SLInsert(ps, 0, elem);
}

void SLPopFront(SL* ps)
{
	/*assert(ps->size > 0);
	int begin = 1;
	while (begin < ps->size)
	{
		ps->arr[begin - 1] = ps->arr[begin];
		begin++;
	}
	ps->size--;*/
	SLErase(ps, 0);
}

int SLFind(SL* ps, int elem)
{
	int pos = 0;
	while (pos < ps->size)
	{
		if (ps->arr[pos] == elem)
		{
			return pos;
		}
		pos++;
	}
	return -1;
}

void SLInsert(SL* ps, int  pos, int elem)
{
	SLCheckCapacity(ps);
	int end = ps->size-1;
	while (end >= pos)
	{
		ps->arr[end + 1] = ps->arr[end];
		end--;
	}
	ps->arr[pos] = elem;
	ps->size++;
}


void SLErase(SL* ps,int  pos)
{
	assert(pos >= 0 && pos < ps->size);
	int begin = pos + 1;
	while (begin < ps->size)
	{
		ps->arr[begin - 1] = ps->arr[begin];
		begin++;
	}
	ps->size--;
}